package cn.wikiflyer.beikang.act;

import java.lang.reflect.Type;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import cn.wikiflyer.beikang.IApplication;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.home.IntentBean;
import cn.wikiflyer.beikang.act.home.NewsAct;
import cn.wikiflyer.beikang.act.study.StudyVideoInfoAct;
import cn.wikiflyer.beikang.act.study.bean.BKStudyListBean;
import cn.wikiflyer.beikang.act.study.bean.BKStudyListBean.StudyItemBean;
import cn.wikiflyer.beikang.act.study.bean.CategoryBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.utils.WKTimeUtils;
import cn.wk.libs4a.view.list.WKListViewInterface;
import cn.wk.libs4a.view.list.WKListViewUtils;
import cn.wk.libs4a.view.list.WKViewHolder;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class TabStudyUIUtils extends Fragment implements OnItemClickListener,
		WKListViewInterface {

	// 当前index
	private int curIdx = 0;
	private CategoryBean bean = null;

	private WKListViewUtils<StudyItemBean, BKStudyListBean> listUtils;

	public TabStudyUIUtils(int idx) {
		this.curIdx = idx;
	}

	public IApplication app() {
		return (IApplication) getActivity().getApplicationContext();
	}

	public static TabStudyUIUtils newInstance(int idx) {
		TabStudyUIUtils study = new TabStudyUIUtils(idx);
		return study;
	}

	public void update(CategoryBean bean) {
		Log.e("wj", "" + bean.catid + " - " + bean.catname);
		this.bean = bean;
		listUtils.updateData();
		// return this;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		listUtils = new WKListViewUtils<StudyItemBean, BKStudyListBean>() {
		};
		listUtils.init(getActivity(), this);
		listUtils.listView.setOnItemClickListener(this);
		listUtils.updateData();
		return listUtils.pullRefreshListView;
	}

	@Override
	public String getData(boolean... isLocal) {
		return app().net.getStudyList("" + app().studyCates.get(curIdx).catid,
				listUtils.page, listUtils.pageCount, isLocal);
	}

	@Override
	public void getDataBack(String result, boolean isLocal) {
		Type cvbType = new TypeToken<BKBaseBean<BKStudyListBean>>() {
		}.getType();
		Gson gson = new Gson();
		BKBaseBean<BKStudyListBean> base = gson.fromJson(result, cvbType);
		listUtils.bean = base;
		// 使用数据
		listUtils.list.addAll(listUtils.bean.data.list);
		listUtils.adapter.notifyDataSetChanged();
	}

	@Override
	public WKViewHolder createViewHolder(View arg1) {
		return new MyViewHolder(arg1);
	}

	@Override
	public int getItemViewId() {
		return R.layout.act_study_list_item;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		StudyItemBean b = listUtils.list.get(arg2 - 1);
		Intent intent = null;
		if (b.typeid == 1) {
			intent = new Intent(getActivity(), StudyVideoInfoAct.class);
			intent.putExtra("bean", b);
			intent.putExtra("title", "学习");
			startActivity(intent);
		} else {
			IntentBean mIBean = new IntentBean(b.title, b.thumb, b.description,
					b.inputtime + "", b.catid, b.id);
			Intent i = new Intent(getActivity(), NewsAct.class);
			i.putExtra("bean", mIBean);
			i.putExtra("title", "学习");
			getActivity().startActivity(i);
		}
	}

	/**
	 * Holder数据保存类
	 */
	class MyViewHolder extends WKViewHolder<StudyItemBean> {
		ImageView img;
		TextView title;
		TextView time;
		TextView read_count;
		ImageView is_video;

		// UI
		public MyViewHolder(View convernView) {
			img = (ImageView) convernView.findViewById(R.id.img);
			title = (TextView) convernView.findViewById(R.id.study_title);
			time = (TextView) convernView.findViewById(R.id.study_time);
			read_count = (TextView) convernView
					.findViewById(R.id.study_readcount);
			is_video = (ImageView) convernView.findViewById(R.id.study_isVideo);

			// 暂时不要阅读次数
			read_count.setVisibility(View.GONE);
		}

		// Data
		public void setData(StudyItemBean bean) {
			title.setText(bean.title);
			time.setText(WKTimeUtils
					.getTimeFromLongEndWithMM((bean.inputtime * 1000))); // 兑换人数
			read_count.setText(bean.posids + ""); // 诚信币数
			if (bean.typeid == 1) {
				is_video.setVisibility(View.VISIBLE);
			} else {
				is_video.setVisibility(View.INVISIBLE);
			}
			if (bean.thumb != null && !bean.thumb.equals("")) {
				img.setVisibility(View.VISIBLE);
				app().imageLoader.displayImage(bean.thumb, img);
			} else {
				img.setVisibility(View.GONE);
			}

			// app().imageLoader.displayImage(bean.logo.replace(" ",
			// "%20"),img);
		}
	}

}
